package com.medical.data.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.medical.data.domain.FollowUpFormToday;
import com.medical.data.domain.FollowUpPatient;
import com.medical.data.domain.HospitalInformation;
import com.medical.data.domain.Patient;
import com.medical.data.service.*;
import com.medical.data.vo.FollowUpVo;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class FollowUpFormServiceImpl implements FollowUpFormService {

    @Resource
    private FollowUpFormTodayService followUpFormTodayService;
    @Resource
    private FollowUpFormPatientService followUpFormPatientService;
    @Resource
    private HospitalInformationService hospitalInformationService;

    @Resource
    private PatientService patientService;

    /**
     * 获取随访概况
     * @param doctorId
     * @return
     */
    @Override
    public Map<String, Integer> getFollowUpPoints(String doctorId) {
        //构造返回值
        Map<String, Integer> map=new HashMap<>();
        LambdaQueryWrapper<FollowUpFormToday> query = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(doctorId)){
            query.eq(FollowUpFormToday::getDoctorId,doctorId);
        }
        //统计今天的数据
        query.eq(FollowUpFormToday::getFollowUpTime,LocalDate.now());
        List<FollowUpFormToday> list = followUpFormTodayService.list(query);
        Map<Integer, List<FollowUpFormToday>> collect = list.parallelStream().collect(Collectors.groupingBy(FollowUpFormToday::getStatus));
        if(CollectionUtils.isNotEmpty(collect.get("1"))){
            map.put("deal",collect.get("1").size());
        }else{
            map.put("deal",0);
        }
        if(CollectionUtils.isNotEmpty(collect.get("0"))){
            map.put("notDeal",collect.get("0").size());
        }else{
            map.put("notDeal",0);
        }
        return  map;
    }

    /**
     * 获取随访日历
     */
    @Override
    public Map<String, Integer> getFollowUpMenology(String doctorId) {
        //构造返回值
        Map<String, Integer> map=new HashMap<>();
        LambdaQueryWrapper<FollowUpPatient> patientQuery = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(doctorId)){
            patientQuery.eq(FollowUpPatient::getDoctorId,doctorId);
        }
        List<FollowUpPatient> list =followUpFormPatientService.list(patientQuery);
        list.stream().forEach(item->{
            String time  = item.getFollowUpTime();
            //转为字符串时间
/*            String time = DateUtil.format(followUpTime,"yyyyMMdd");*/
            if(map.get(time)==null){
                map.put(time,1);
            }else{
                map.put(time,map.get(time)+1);
            }
        });
        return map;
    }

    /**
     * 获取今日随访列表
     */
    @Override
    public List<FollowUpVo> getFollowUpList(String doctorId) {
        LambdaQueryWrapper<FollowUpFormToday> todayQuery = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(doctorId)){
            todayQuery.eq(FollowUpFormToday::getDoctorId,doctorId);
        }
        todayQuery.eq(FollowUpFormToday::getStatus, BooleanUtils.toInteger(Boolean.FALSE));
        //获取今日还未随访的
        List<FollowUpFormToday> list = followUpFormTodayService.list(todayQuery);
        //构造返回值
        List<FollowUpVo> resList=new LinkedList<>();
        list.stream().forEach(item->{
            FollowUpVo followUpVo = new FollowUpVo();
            //患者id
            String patientId = item.getPatientId();
            Patient patient = patientService.list(new LambdaQueryWrapper<Patient>().eq(Patient::getId, patientId)).get(0);
            //今日随访id 用于改变随访状态
            followUpVo.setId(item.getId());
            //病例编号
            followUpVo.setRecordNumber(patient.getRecordNumber());
            //获取医院信息
            HospitalInformation hospital = hospitalInformationService.getById(patient.getHospitalId());
            followUpVo.setHospitalName(hospital.getHsName());
            followUpVo.setPatientName(patient.getPatientName());
            followUpVo.setPatientPhone(patient.getPatientPhone());
            followUpVo.setStatusName("未完成");
            followUpVo.setDoctorName(item.getDoctorName());
            resList.add(followUpVo);
        });
        return resList;
    }

    @Override
    public boolean changeFollowUpStatus(String id) {
        LambdaQueryWrapper<FollowUpFormToday> todayQuery = new LambdaQueryWrapper<>();
        todayQuery.eq(FollowUpFormToday::getId,id);
        FollowUpFormToday today = followUpFormTodayService.getOne(todayQuery);
        //改变随访状态
        today.setStatus(BooleanUtils.toInteger(Boolean.TRUE));
        return followUpFormTodayService.updateById(today);
    }
}
